package 剑指offer;

import java.util.LinkedList;

public class A1 {
    public int divide(int a, int b) {
            if (a==Integer.MIN_VALUE&&b==-1) {
                return Integer.MAX_VALUE;
            }
            if (b==1) {
                return a;
            }
            if (b==-1) {
                return -a;
    
            }
    
            int flag=1;
            if (a>0) {
                flag=-flag;
                a=-a;
            }
            if (b>0) {
                flag=-flag;
                b=-b;
            }
            LinkedList<Integer> data=new LinkedList<>();
            a-=b;
            a=-a;
            b=-b;
            if (a<0) {
                return 0;
            }
            int middle=b;
            int count=1;
            while (middle<=a) {
                data.addFirst(middle);
                a-=middle;
                count+=data.size();
                middle+=b;
            }
            while (data.size()>0) {
                while (a>=data.peekFirst()) {
                    count+=data.size();
                    a-=data.peekFirst();
                }
                data.pollFirst();
            }
            if (flag>0) {
                return count;
            }
            return -count;
        }
}
